package com.asiainfo.operationdb;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.asiainfo.util.JDBCUtil;

public class CheckData {
	public Connection conn = null;
	public PreparedStatement ps = null;
	public Statement stmt = null;
	public ResultSet rs = null;

	/**
	 * 获取数据库中会员ID
	 * 
	 * @param userName
	 *            ，通过用户名获取
	 * @return
	 */
	public int getID(String name, String sql) {
		int result = 0;
		conn = JDBCUtil.getConnection();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				if (rs.getString(2).equals(name)) {
					result = rs.getInt(1);
					break;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, stmt, ps, rs);
		}
		return result;
	}

	/**
	 * 判断预定的课程是否存在
	 * 
	 * @param SheduleID
	 * @param sql
	 * @return
	 */
	public boolean isSheduleID(String SheduleID, String sql) {
		conn = JDBCUtil.getConnection();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				if (rs.getString(1).equals(SheduleID)) {
					return true;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, stmt, ps, rs);
		}
		return false;
	}

	/**
	 * 获取教练名称
	 * 
	 * @param scheduleID
	 * @param sql
	 * @return
	 */
	public String getCoachName(String scheduleID, String sql) {
		String result = "";
		conn = JDBCUtil.getConnection();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				if (rs.getString(1).equals(scheduleID)) {
					result = rs.getString(5);
					break;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, stmt, ps, rs);
		}
		return result;
	}

	/**
	 * 获取课程名称
	 * 
	 * @param scheduleID
	 * @param sql
	 * @return
	 */
	public String getCourseName(String scheduleID, String sql) {
		String result = "";
		conn = JDBCUtil.getConnection();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				if (rs.getString(1).equals(scheduleID)) {
					result = rs.getString(4);
					break;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, stmt, ps, rs);
		}
		return result;
	}
}
